﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using ToRTF;
using ILookintoControl.Ionic.Utils;
using net.kvdb.webdav;
using Org.Mentalis.Network;
//using Duplicati.Library.Backend;
namespace ILookintoControl
{
    public partial class ILViewerControl : UserControl
    {
        //WordDocHandler wdh;
        public ILViewerControl()
        {
            InitializeComponent();

            ContextMenu popUpMenu = new ContextMenu();

            popUpMenu.MenuItems.Add("Open Repository");//, new EventHandler(PopUp_Clicked));
            popUpMenu.MenuItems.Add("Import files", new EventHandler(ImportEvent));
            popUpMenu.MenuItems.Add("Close Repository");//, new EventHandler(PopUp_Clicked));

            this.fileMenu.ContextMenu = popUpMenu;
            
            
            this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyDown);
            this.htmlPanel1.OutRichTextBox = this.richTextBox1;
            //wdh = new WordDocHandler();
            //wdh.Init();
            //this.htmlPanel1.wdh = wdh;
            this.textBox1.Focus();
            this.GotFocus +=new EventHandler(ILViewerControl_GotFocus);
        }

        private void goButton_Click(object sender, EventArgs e)
        {
            doSearch();
        }
        private void textBox1_KeyDown(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;
            if (c == (char)Keys.Return)
            {
                doSearch();
            }
        }
        private void doSearch()
        {
                if (textBox1.Text.Trim().Length != 0)
            {


                WebClient wc = new WebClient();

                String html = wc.DownloadString(GlobalVar.webUrl + "/viewsearch.jsp?q=" + textBox1.Text.Trim());
                int begin = html.IndexOf("<table");
                int end = html.LastIndexOf("<br clear=all>");
                if (begin > 0 && end > 0)
                {
                    html = html.Substring(begin, end - begin);
                    this.htmlPanel1.Text =  html;
                    //this.htmlPanel1.Text = begin + " " + end;
                }
                else
                {
                    begin = html.IndexOf("Your search");
                    this.htmlPanel1.Text = html.Substring(begin);
                }


            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string mystring = "c:\\zwu\\temp\\my.rtf";
            //wdh.SaveAs("http://hraunfoss.fcc.gov/edocs_public/attachmatch/FCC-05-116A1.doc", ref mystring, "rtf");
            this.richTextBox1.LoadFile(mystring, RichTextBoxStreamType.RichText);
            //wdh.CleanUp();
        }

        private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void fileMenu_Click(object sender, EventArgs e)
        {
            this.fileMenu.ContextMenu.Show(fileMenu, new Point(0, fileMenu.Bottom - 4));
        }

       

        //FolderBrowserDialog dlg = new FolderBrowserDialog();
        FolderBrowserDialogEx dlg = new FolderBrowserDialogEx();
        private void ImportEvent(Object sender, EventArgs e)
        {

            dlg.SelectedPath = @"c:\zwu\temp";
            dlg.ShowBothFilesAndFolders = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                //Console.WriteLine(dlg.FileName);

                FileInfo fi = new FileInfo(dlg.SelectedPath);
                if (!fi.Exists)
                {
                    DirectoryInfo di = new DirectoryInfo(dlg.SelectedPath);

                }
                upload(dlg.SelectedPath);
                //File file = new File(dlg.SelectedPath);
            }


        }

        private void upload(String file)
        {


            Url u = new Url(GlobalVar.webDav);
            /*wc.Server = u.Protocol +"://"+ u.Host;
            wc.Port = u.Port;
            wc.BasePath = u.Path;
            wc.Pass = GlobalVar.password;
            wc.User = GlobalVar.user;*/
            String url = u.Protocol + "://" + GlobalVar.user
                + ":" + GlobalVar.password + "@" + u.Host + ":" + u.Port + "/" + u.Path;

            //wc.List();

            WebDAVClient wdc = new WebDAVClient();
            wdc.BasePath = u.Path;
            wdc.Server = u.Protocol + "://" + u.Host;
            wdc.Port = u.Port;
            wdc.User = GlobalVar.user;
            wdc.Pass = GlobalVar.password;

            

            autoResetEvent = new AutoResetEvent(false);
            wdc.UploadComplete += new UploadCompleteDel(c_UploadComplete);


            try
            {
                wdc.CreateDirComplete += new CreateDirCompleteDel(c_CreateDirComplete);

                wdc.CreateDir("abc2"); //remoteFile.Substring(0, remoteFile.LastIndexOf("/")));
            }
            catch (Exception e) { }

            autoResetEvent.WaitOne();

            autoResetEvent = new AutoResetEvent(false);
            //wdc.BasePath = "/"; // wdc.BasePath;
            //wdc.Upload(file, remoteFile);
            //WEBDAV wc = new WEBDAV(url + "/abc", new Dictionary<string, string>());
            String remoteFile = file.Substring(file.IndexOf("\\") + 1);
            remoteFile = remoteFile.Replace("\\", "/");
            wdc.Upload(file, "remoteFile3.rtf");
            autoResetEvent.WaitOne();

            //wc.Put(remoteFile, file);
        }

        private void ILViewerControl_GotFocus(object sender, EventArgs e)
        {
            MessageBox.Show("abc");
            this.textBox1.Focus();
        }
        static AutoResetEvent autoResetEvent;
        static void c_UploadComplete(int statusCode, object state)
        {
           // Debug.Assert(statusCode == 201);
            autoResetEvent.Set();
        }

        static void c_CreateDirComplete(int statusCode)
        {
            // Debug.Assert(statusCode == 201);
            autoResetEvent.Set();
        }

        private void htmlPanel1_Click(object sender, EventArgs e)
        {

        }

    }

}
